Passed
Push — master ( 3278dc...27a5bd )
by Kevin Van
03:53 queued 11s
created

Matches.tsx ➔ getData   B

Complexity

Conditions 7

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 8
c 0
b 0
f 0
cc 7
1
import React, { FunctionComponent, useEffect, useState } from "react"
2
import { graphql, Link, useStaticQuery } from "gatsby"
3
4
import axios from "axios"
5
import LazyLoad from "react-lazyload"
6
import classNames from "classnames"
7
import Moment from "moment-timezone"
8
import "moment/locale/nl-be"
9
10
import { mapPsdStatus, mapPsdStatusShort } from "../scripts/helper"
11
12
import "./Matches.scss"
13
import Icon from "./Icon"
14
import Spinner from "./Spinner"
15
16
const MatchesRow: FunctionComponent<MatchesRowProps> = ({ match }: MatchesRowProps) => {
17
  const d = Moment.tz(match.date, `Europe/Brussels`)
18
  const date = d.format(`dddd D MMMM YYYY`)
19
  const time = d.format(`HH:mm`)
20
  const matchPlayed =
21
    ((match.status === 0 || match.status === null) && match.goalsHomeTeam !== null && match.goalsAwayTeam !== null) ||
22
    false
23
24
  return (
25
    <article>
26
      <header className="matches__calendar__title">
27
        <h3>
28
          <span className="matches__calendar__date">{date}</span>
29
          <span className="matches__calendar__separator">|</span>
30
          <span className="matches__calendar__type">{match.competitionType}</span>
31
        </h3>
32
      </header>
33
      <main className="matches__calendar__main">
34
        <div
35
          className={classNames(`matches__calendar__team`, `matches__calendar__team--home`, {
36
            "matches__calendar__team--winner": matchPlayed && match.goalsHomeTeam > match.goalsAwayTeam,
37
          })}
38
        >
39
          {match.homeClub?.name}
40
41
          <LazyLoad debounce={false}>
42
            <img
43
              src={match.homeClub?.logo}
44
              alt={match.homeClub?.name}
45
              className="matches__calendar__logo matches__calendar__logo--home"
46
            />
47
          </LazyLoad>
48
        </div>
49
50
        <div className="matches__calendar__score">
51
          {match.status !== 0 && (
52
            <span title={mapPsdStatus(match.status) || ``}>{mapPsdStatusShort(match.status)}</span>
53
          )}
54
          {(match.status === 0 || match.status === null) && !matchPlayed && <span>{time}</span>}
55
          {matchPlayed && (
56
            <span>
57
              {match.goalsHomeTeam} - {match.goalsAwayTeam}
58
            </span>
59
          )}
60
        </div>
61
62
        <div
63
          className={classNames(`matches__calendar__team`, `matches__calendar__team--away`, {
64
            "matches__calendar__team--winner": matchPlayed && match.goalsHomeTeam < match.goalsAwayTeam,
65
          })}
66
        >
67
          <LazyLoad debounce={false}>
68
            <img
69
              src={match.awayClub?.logo}
70
              alt={match.awayClub?.name}
71
              className="matches__calendar__logo matches__calendar__logo--away"
72
            />
73
          </LazyLoad>
74
          {match.awayClub?.name}
75
        </div>
76
77
        <Link to={`/game/${match.id}`} className="matches__calendar__link">
78
          <Icon icon="fa-info-circle" />
79
        </Link>
80
      </main>
81
    </article>
82
  )
83
}
84
85
const Matches: FunctionComponent<MatchesProps> = ({ teamId }: MatchesProps) => {
86
  const [data, setData] = useState<Match[]>([])
87
88
  const {
89
    site: {
90
      siteMetadata: { kcvvPsdApi },
91
    },
92
  }: MatchesQueryData = useStaticQuery(graphql`
93
    {
94
      site {
95
        siteMetadata {
96
          kcvvPsdApi
97
        }
98
      }
99
    }
100
  `)
101
102
  Moment.locale(`nl-BE`)
103
104
  useEffect(() => {
105
    async function getData() {
106
      const response = await axios.get(`${kcvvPsdApi}/matches/${teamId}`)
107
      setData(response.data)
108
    }
109
    getData()
110
  }, [])
111
112
  return (
113
    <div className={`matches__wrapper`}>
114
      {data.length > 0 || <Spinner />}
115
      {data
116
        .sort((a, b) => a.timestamp - b.timestamp)
117
        .map((match, i) => (
118
          <MatchesRow match={match} key={i} />
119
        ))}
120
    </div>
121
  )
122
}
123
124
export default Matches
125